﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace VFTray.Mi
{
	class DESHelper
	{

		internal static byte[] Encrypt(byte[] data, string pwd)
		{
			string skey = pwd.Substring(0, pwd.Length / 2);
			string siv = pwd.Substring(pwd.Length / 2, pwd.Length / 2);
			var key = Mi.CodeHelper.FromBase32String(skey);
			var ivs = Mi.CodeHelper.FromBase32String(siv);

			DESCryptoServiceProvider des = new DESCryptoServiceProvider();
			des.Key = key;
			des.IV = ivs;
			ICryptoTransform encrypt = des.CreateEncryptor(des.Key, des.IV);
			var by_en = encrypt.TransformFinalBlock(data, 0, data.Length);
			return by_en;
		}

		internal static byte[] Decrypt(byte[] data, string pwd)
		{
			string skey = pwd.Substring(0, pwd.Length / 2);
			string siv = pwd.Substring(pwd.Length / 2, pwd.Length / 2);
			var key = CodeHelper.FromBase32String(skey);
			var ivs = CodeHelper.FromBase32String(siv);

			DESCryptoServiceProvider des = new DESCryptoServiceProvider();
			des.Key = key;
			des.IV = ivs;
			ICryptoTransform encrypt = des.CreateDecryptor(des.Key, des.IV);
			var by_de = encrypt.TransformFinalBlock(data, 0, data.Length);
			return by_de;
		}
	}
}
